package com.ruoyi.am.controller;

import java.util.List;

import com.ruoyi.am.domain.AmCarPlace;
import com.ruoyi.am.service.IAmCarPlaceService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.AmRoute;
import com.ruoyi.am.service.IAmRouteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 救护信息Controller
 * 
 * @author hkkk
 * @date 2021-05-27
 */
@Controller
@RequestMapping("/am/route")
public class AmRouteController extends BaseController
{
    private String prefix = "am/route";

    @Autowired
    private IAmRouteService amRouteService;
    @Autowired
    private IAmCarPlaceService amCarPlaceService;

    @RequiresPermissions("am:route:view")
    @GetMapping()
    public String route()
    {
        return prefix + "/route";
    }

    /**
     * 查询救护信息列表
     */
    @RequiresPermissions("am:route:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmRoute amRoute)
    {
        startPage();
        List<AmRoute> list = amRouteService.selectAmRouteList(amRoute);
        return getDataTable(list);
    }

    /**
     * 导出救护信息列表
     */
    @RequiresPermissions("am:route:export")
    @Log(title = "救护信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmRoute amRoute)
    {
        List<AmRoute> list = amRouteService.selectAmRouteList(amRoute);
        ExcelUtil<AmRoute> util = new ExcelUtil<AmRoute>(AmRoute.class);
        return util.exportExcel(list, "route");
    }

    /**
     * 新增救护信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存救护信息
     */
    @RequiresPermissions("am:route:add")
    @Log(title = "救护信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmRoute amRoute)
    {
        //此处缺少使用系统算法给司机分配任务
        AmCarPlace CarPlace = new AmCarPlace();
        CarPlace.setDriverId(amRoute.getDriverId());
        List<AmCarPlace> list = amCarPlaceService.selectAmCarPlaceList(CarPlace);
        if (list.size() == 1) {
            AmCarPlace amCarPlace = list.get(0);
            int status = Integer.parseInt(amCarPlace.getStatus());
            AmRoute route = new AmRoute();
            route.setDriverId(amRoute.getDriverId());
            route.setStatus("-1");
            if((status < 0 || status == 2) && amRouteService.selectAmRouteList(route).size() == 0){
                amCarPlace.setStatus("-1");
                amCarPlaceService.updateAmCarPlace(amCarPlace);
                amRoute.setStatus("-1");
                amRoute.setStartTime(DateUtils.getNowDate());
                return toAjax(amRouteService.insertAmRoute(amRoute));
            }
            else {
                return AjaxResult.error("已无可分配救护车资源！！！");
            }
        }
        else {
            return AjaxResult.error("未查询到该司机位置信息！");
        }
    }

    /**
     * 修改救护信息
     */
    @GetMapping("/edit/{routeId}")
    public String edit(@PathVariable("routeId") Long routeId, ModelMap mmap)
    {
        AmRoute amRoute = amRouteService.selectAmRouteById(routeId);
        mmap.put("amRoute", amRoute);
        return prefix + "/edit";
    }

    /**
     * 修改保存救护信息
     */
    @RequiresPermissions("am:route:edit")
    @Log(title = "救护信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmRoute amRoute)
    {
        long id = amRoute.getDriverId();
        List<AmCarPlace> list = amCarPlaceService.selectAmCarPlaceList(new AmCarPlace(id));
        if(list.size()==1){
            AmCarPlace carPlace = list.get(0);
            carPlace.setStatus(amRoute.getStatus());
            amCarPlaceService.updateAmCarPlace(carPlace);
        }
        else {
            logger.error("查询地理位置信息出错！");
        }
        return toAjax(amRouteService.updateAmRoute(amRoute));
    }

    /**
     * 删除救护信息
     */
    @RequiresPermissions("am:route:remove")
    @Log(title = "救护信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amRouteService.deleteAmRouteByIds(ids));
    }
}
